
namespace TFSChangeLog.Operations.Services
{
    using System;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Threading;
    using TFSChangeLog.DataAccess;
    using TFSChangeLog.DataModel;
    using TFSChangeLog.ViewModel;

    /// <summary>Summary description for <see cref="SelectProjectService"/> class.</summary>
    public static class SelectProjectService
    {
        /// <summary>Summary description for <c>SelectProjectService</c> method.</summary>
        /// <param name="singleIterationList">TODO: Summary description for <c>singleIterationList</c> parameter of type <c>int[]</c>.</param>
        /// <param name="viewModel">TODO: Summary description for <c>viewModel</c> parameter of type <c>TFSChangeLog.ViewModel.ProgressDialogViewModel</c>.</param>
        public static void DoWork(int[] singleIterationList, ProgressDialogViewModel viewModel)
        {
            // Get a cancellation token
            var loopOptions = new ParallelOptions();
            loopOptions.CancellationToken = viewModel.TokenSource.Token;
            try
            {
                Parallel.ForEach(singleIterationList, loopOptions, t => GetBranchInformation(viewModel));

// Parallel.Invoke(loopOptions, () => GetBranchInformation(viewModel));
            }
            catch (OperationCanceledException)
            {
                var ShowCancellationMessage = new Action(viewModel.ShowCancellationMessage);
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, ShowCancellationMessage);
            }
            catch (Exception e)
            {
                var ShowCancellationMessage = new Action(viewModel.ShowCancellationMessage);
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, ShowCancellationMessage);
            }
        }

        /// <summary>Summary description for <c>SelectProjectService</c> method.</summary>
        /// <param name="p_ViewModel">TODO: Summary description for <c>p_ViewModel</c> parameter of type <c>TFSChangeLog.ViewModel.ProgressDialogViewModel</c>.</param>
        public static void GetBranchInformation(ProgressDialogViewModel p_ViewModel)
        {
            MainWindowViewModel m_viewModel = p_ViewModel.GetMainWindowViewModel();
            Branch tfsBranchHierarchy = null;
            if (TFSChangeLog.DataAccess.TFSChangeLogStaticConfigs.IsRunningFromCommandPrompt == false)
                {
                SetProgressBarMessage(p_ViewModel);
                }

            tfsBranchHierarchy = BranchDataAccess.GetBranchTree(m_viewModel);
            if (TFSChangeLog.DataAccess.TFSChangeLogStaticConfigs.IsRunningFromCommandPrompt == false)
                if (p_ViewModel.TokenSource.Token.IsCancellationRequested)
                {
                    return;
                }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

            m_viewModel.UpdateBranchTreeStructure(tfsBranchHierarchy);

            m_viewModel.TFSServerName = TFSConnection.GetTFSServerURL();
            m_viewModel.TFSProjectName = TFSConnection.GetTFSSelectedProjectName();
            m_viewModel._currentUser = CurrentUserDataAccess.GetAuthenticatedCurrentUserIdentity();
        }

        /// <summary>
        /// Set Progress Bar Message.
        /// </summary>
        /// <param name="viewModel">TODO: Summary description for <c>viewModel</c> parameter of type <c>TFSChangeLog.ViewModel.ProgressDialogViewModel</c>.</param>
        private static void SetProgressBarMessage(ProgressDialogViewModel viewModel)
        {
            string message = "Quering TFS for Branch List";
            var SetProgressMessage = new Action<string>(viewModel.SetProgressMessage);
            try
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, SetProgressMessage, message);
            }
            catch (Exception ex)
            {
            }
        }
    }
}